// pages/detail/detail.js
import api from '../../utils/api.js'
var app = getApp();
const util = require('../../utils/fenxiang.js');
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        good: "",
        details: "",
        tuijian: [],
        url: config.imgUrl + "/images/shoucang-off.png",
        num: "",
        show: true,
        images: {},
        is_auth: false,
        imgUrl: config.imgUrl,
    },
    //授权 初始化
    likesClick() {
        this.setData({
            is_auth: true
        })
        this.add()
    },
    // 获取商品详情接口
    getOne(obj) {
        http.request({
            url: api.getOnE,
            data: obj,
            success: res => {
                this.setData({
                    good: res.data,
                    details: JSON.parse(res.data.details)
                })
                wx.setNavigationBarTitle({
                    title: res.data.name,
                })
            }
        })
    },
    //通过食材获取相关所有相关菜谱接口
    getPidCookbooks(obj, order) {
        http.request({
            url: api.getPidCookbookS,
            data: obj,
            success: res => {
                console.log(res)
                if (order == 1) {
                    if (res.code == 201) {
                        this.setData({
                            show: false
                        })
                    } else {
                        this.setData({
                            tuijian: res.data
                        })
                    }
                } else {
                    if (res.code == 201) {
                        wx.showToast({
                            title: '该商品暂无相关推荐菜谱',
                            icon: "none"
                        })
                    } else {
                        wx.setStorageSync("search-list", res.data);
                        wx.switchTab({
                            url: "/pages/index/index"
                        })
                    }
                }

            }
        })
    },
    //加入菜篮接口
    shopping_cart_add(obj) {
        http.request({
            url: api.shoppingCartAdd,
            data: obj,
            success: res => {
                console.log(res)
                if (res.code == 200) {
                    var num = this.data.num * 1 + 1
                    this.setData({
                        num
                    })
                    wx.showToast({
                        title: '添加成功',
                        duration: 1500
                    })
                }
            }
        })
    },

    //跳转下单页面
    toOrder() {
        let selectGoods = wx.getStorageSync("select-goods") || []
        if (selectGoods.length > 0) {
            wx.navigateTo({
                url: "/pages/recommendShopGoodList/recommendShopGoodList?type=0"
            })
        } else {
            wx.showToast({
                title: '您还未选商品',
                icon: "none"
            })
        }
    },
    // 相关菜谱
    xiangguan() {
        app.globalData.id = ""
        let obj = {
            id: this.data.good.id,
            page: 1,
            pagesize: 1000,
        }
        this.getPidCookbooks(obj, 2)
    },
    // 加入菜篮
    add() {
        var good = this.data.good
        var selectGoods = wx.getStorageSync("select-goods") || []
        var item = selectGoods.filter(item => { // 去本地列表里面查找点击的单品
            return item.id == good.id
        })[0]
        if (item) { // 列表里面是否有该单品（有）
            wx.showToast({
                title: '请不要重复添加',
                icon: "none"
            })
        } else { // 列表里面是否有该单品（无）
            wx.showToast({
                title: '添加成功',
                duration: 1500
            })
            good.num = 1
            selectGoods.push(good)
            wx.setStorageSync("select-goods", selectGoods)
        }

    },
    //图片高度自适应
    imageLoad: function (e) {
        var $width = e.detail.width, //获取图片真实宽度
            $height = e.detail.height,
            ratio = $width / $height; //图片的真实宽高比例
        var viewWidth = 750, //设置图片显示宽度
            viewHeight = 750 / ratio; //计算的高度值
        //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
        var images = {
            width: viewWidth,
            height: viewHeight
        }
        this.setData({
            images
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let is_auth = wx.getStorageSync('is_auth');
        if (is_auth) {
            this.setData({
                is_auth: true
            })
        }
        this.getOne({ id: options.id }) // 获取商品详情
        let obj = {
            id: options.id,
            page: 1,
            pagesize: 1000,
        }
        this.getPidCookbooks(obj, 1) // 获取相关推荐菜谱
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this
        wx.getSystemInfo({
            success(res) {
                if (res.model.indexOf("iPhone X") != -1) {
                    that.setData({
                        white: true
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (option) {
        let obj = {
            title: this.data.good.name,
            imageUrl: '',
            path: `/pages/index/index?redirect=/pages/detail/detail&typeShare=1&id=${this.data.good.id}`
        };
        console.log(this.data.good.id)
        console.log(option)
        return (option, obj);
    }
})